package com.oa.core.utils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 参数校验
 * @author syb
 * date 2019/2/11
 */
public class Validate {
    private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory ();

    public static <T> List<String> validate(T t) {
        Validator validator = factory.getValidator ();
        Set<ConstraintViolation<T>> constraintViolations = validator.validate (t);

        List<String> messageList = new ArrayList<> ();
        for (ConstraintViolation<T> constraintViolation : constraintViolations) {
            messageList.add (constraintViolation.getMessage ());
        }
        return messageList;
    }
}
